//
//  XDRecommendDateBaseManager.m
//  Le way walkers
//
//  Created by dlios on 15-5-22.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDRecommendDateBaseManager.h"
#import "XDRecommend.h"
#import "XDDetails.h"
@implementation XDRecommendDateBaseManager

+ (XDRecommendDateBaseManager *)shareInstance
{
    static XDRecommendDateBaseManager *dbManager = nil;
    if (dbManager == nil) {
        dbManager = [[XDRecommendDateBaseManager alloc]init];
    }
    return dbManager;
}




//打开数据库
- (void)openDB
{
    //获取documents文件路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    //拼接数据库路径
    NSString *dbPath = [path stringByAppendingPathComponent:@"xdrecommend.db"];
//     NSString *dbPath = [path stringByAppendingPathComponent:@"xddetails.db"];
    //创建数据库
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    NSLog(@"打开结果:%d,path:%@",result,dbPath);
}




//关闭数据库
- (void)closeDB
{
    sqlite3_close(dbPoint);
}




//创建表
- (void)createTable
{
    
    //1创建sql语句
    NSString *createSQL = @"create table if not exists xdrecommend(cover_image text, name text, popular_place_str text, first_day text, day_count text, image_url text, ids text, num integer primary key autoincrement)";
    
    //    NSString *createSQL = @"create table if not exists xdrecommend(day text, name text, photo text, text text, local_time text, num integer primary key autoincrement)";
    //2执行sql语句
    int result = sqlite3_exec(dbPoint, createSQL.UTF8String, NULL, NULL, nil);
    //3判断
    if (SQLITE_OK == result) {
        NSLog(@"创建表成功");
    }
}




//传入对象信息
- (void)insertInfoWithXDRecommend:(XDRecommend *)recommend
{
    //创建sql语句
    NSString *insertStr = [NSString stringWithFormat:@"insert into xdrecommend(cover_image, name, popular_place_str, first_day, day_count,image_url, ids) values ('%@', '%@', '%@', '%@', '%@', '%@', '%@')",recommend.cover_image,  [recommend.name stringByReplacingOccurrencesOfString:@"'" withString:@" "], recommend.popular_place_str, recommend.first_day, recommend.day_count, recommend.image_url, recommend.ids];
    

    //执行语句
    int result = sqlite3_exec(dbPoint, insertStr.UTF8String, NULL, NULL, nil);
    //判断
    if (SQLITE_OK == result) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}




//查询
- (NSMutableArray *)selectInfo
{
    //1创建sql语句
    NSString *selectSQL = @"select * from xdrecommend";
    //     NSString *selectSQL = @"select * from xddetails";
    //2创建数据库替身
    sqlite3_stmt *stmt = nil;
    //3准备sql语句
    int result = sqlite3_prepare_v2(dbPoint, selectSQL.UTF8String, -1, &stmt, nil);
    //4创建存放返回数据的信息数组
    NSMutableArray *arr = [NSMutableArray array];
    // 5判断查询准备
    if (SQLITE_OK == result) {
        NSLog(@"查询成功");
        //6开始遍历每一行信息
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *cover_image = sqlite3_column_text(stmt, 0);
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *popular_place_str = sqlite3_column_text(stmt, 2);
            const unsigned char *first_day = sqlite3_column_text(stmt, 3);
            const unsigned char *day_count = sqlite3_column_text(stmt, 4);
            const unsigned char *image_url = sqlite3_column_text(stmt, 5);
            const unsigned char *ids = sqlite3_column_text(stmt, 6);
            int num = sqlite3_column_int(stmt, 7);
            // 访问到数据库数据 转化添加在Model中
            XDRecommend *temp = [[XDRecommend alloc]init];
            temp.cover_image = [NSString stringWithUTF8String:(const char *)cover_image];
            temp.name = [NSString stringWithUTF8String:(const char *)name];
            temp.popular_place_str = [NSString stringWithUTF8String:(const char *)popular_place_str];
            temp.first_day = [NSString stringWithUTF8String:(const char *)first_day];
            temp.day_count = [NSString stringWithUTF8String:(const char *)day_count];
            temp.image_url = [NSString stringWithUTF8String:(const char *)image_url];
            temp.ids = [NSString stringWithUTF8String:(const char *)ids];
            temp.num = num;
            //把Model对象添加在数组中
            [arr addObject:temp];
        }
    }else{
        NSLog(@"查询准备失败");
    }
    return arr;
}


- (void)deleteInfoWithXDReconmmendName:(NSString *)name
{
    //创建sql语句
    NSString *deleteSQL = [NSString stringWithFormat:@"delete from xdrecommend where name = '%@'",name];
    //执行语句
    int result = sqlite3_exec(dbPoint, deleteSQL.UTF8String, NULL, NULL, nil);
    if (SQLITE_OK == result) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
    
}




-(void)dropTable
{
    NSString *dropSQL = @"drop table xdrecommend";
    int result = sqlite3_exec(dbPoint, dropSQL.UTF8String, NULL, NULL, nil);
    if (SQLITE_OK == result) {
        NSLog(@"删除表成功");
    }else{
        NSLog(@"删除表失败");
    }
}











@end
